﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class RoleApp : IRole
    {
        private string dbname = "Systems";

        public void Add(RoleEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_Role(");
            builder.Append("ChainNum,DetpId,RoleName,RoleMoney,RoleText,RoleLevel,IsSuperPop,AutomaticNumber");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.DetpId + "',");
            builder.Append("'" + model.RoleName + "',");
            builder.Append("" + model.RoleMoney + ",");
            builder.Append("'" + model.RoleText + "',");
            builder.Append("" + model.RoleLevel + ",");
            builder.Append("" + model.IsSuperPop + ",");
            builder.Append("" + model.AutomaticNumber + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int RoleId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_Role ");
            builder.Append(" where RoleId=" + RoleId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Role ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by RoleId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "RoleId", "T_Role", swhere);
        }

        public RoleEnt GetModel(int RoleId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select RoleId,ChainNum,DetpId,RoleName,RoleMoney,RoleText,RoleLevel,IsSuperPop,AutomaticNumber from T_Role ");
            builder.Append(" where RoleId=" + RoleId);
            RoleEnt ent = new RoleEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.RoleId = RoleId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.DetpId = table.Rows[0]["DetpId"].ToString();
                ent.RoleName = table.Rows[0]["RoleName"].ToString();
                if (table.Rows[0]["RoleMoney"].ToString() != "")
                {
                    ent.RoleMoney = decimal.Parse(table.Rows[0]["RoleMoney"].ToString());
                }
                ent.RoleText = table.Rows[0]["RoleText"].ToString();
                if (table.Rows[0]["RoleLevel"].ToString() != "")
                {
                    ent.RoleLevel = int.Parse(table.Rows[0]["RoleLevel"].ToString());
                }
                if (table.Rows[0]["IsSuperPop"].ToString() != "")
                {
                    ent.IsSuperPop = int.Parse(table.Rows[0]["IsSuperPop"].ToString());
                }
                if (table.Rows[0]["AutomaticNumber"].ToString() != "")
                {
                    ent.AutomaticNumber = int.Parse(table.Rows[0]["AutomaticNumber"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_Role ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "RoleId");
        }

        public void Update(RoleEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_Role set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("DetpId='" + model.DetpId + "',");
            builder.Append("RoleName='" + model.RoleName + "',");
            builder.Append("RoleMoney=" + model.RoleMoney + ",");
            builder.Append("RoleText='" + model.RoleText + "',");
            builder.Append("RoleLevel=" + model.RoleLevel + ",");
            builder.Append("IsSuperPop=" + model.IsSuperPop + ",");
            builder.Append("AutomaticNumber=" + model.AutomaticNumber + "");
            builder.Append(" where RoleId=" + model.RoleId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

